"""
進入爱逛卖家版
"""
import time

import yaml
from uiautomator2 import xpath


from page_object.guang_page import GuangPage
from page_object.living_page import LivingPage
from resource.path_info import *

"""
创建直播
"""
class CreateBroadcastPage(GuangPage):
    """
    创建直播
    """
    def broadcast_create(self, d, title):
        print("broadcast_create")
        self.broadcast_take_head_image(d)
        self.broadcast_take_poster_image(d)
        self.broadcast_input_title(d,title)
        self.broadcast_other_config(d)
        d.xpath(start_broadcast_btnNext_xpath).click()
        self.give_camera_permission(d)
        d.xpath(start_broadcast_xpath).click()
        d.xpath(end_live_broadcast_btn_xpath).wait(timeout=10.0)
        self.living_next_step(d)
        return LivingPage()

    """
    修改封面
    """
    def broadcast_take_head_image(self, d):
        print("broadcast_take_head_image")
        d.xpath(start_broadcast_head_image).click()
        d.xpath(image_source_self_xpath).click()
        # 获取相册权限
        time.sleep(1)
        if d.xpath(to_give_permission_xpath).exists:
            d.xpath(to_give_permission_xpath).click()
            d.xpath(give_permission_using_xpath).click()
            d.xpath(to_take_photo_xpath).wait(timeout=3.0)
        time.sleep(1)
        d.xpath(to_take_photo_xpath).click()
        # 获取相机权限
        if d.xpath(to_give_permission_xpath).exists:
            d.xpath(to_give_permission_xpath).click()
            d.xpath(give_permission_using_xpath).click()
        d.xpath(take_photo_xpath).click()
        d.xpath(pre_confirm_photo_xpath).wait(timeout=3.0)
        d.xpath(pre_confirm_photo_xpath).click()
        d.xpath(confirm_photo_xpath).click()
        return self

    """
   修改海报
   """

    def broadcast_take_poster_image(self, d):
        print("broadcast_take_poster_image")
        d.xpath(start_broadcast_poster_image).click()
        d.xpath(image_source_self_xpath).click()
        time.sleep(2)
        d.xpath(to_take_photo_xpath).click()
        d.xpath(take_photo_xpath).click()
        d.xpath(pre_confirm_photo_xpath).wait(timeout=3.0)
        d.xpath(pre_confirm_photo_xpath).click()
        d.xpath(confirm_photo_xpath).click()
        return self


    """
    输入标题及介绍
    """
    def broadcast_input_title(self, d, title):
        print("broadcast_input_title")
        d.xpath(start_broadcast_title_xpath).wait(timeout=3.0)
        d.xpath(start_broadcast_title_xpath).click()
        d(focused=True).send_keys(title)
        if d.xpath(sougou_input_title_xpath).exists:
            d.press("back")
        # d.swipe(0.7, 0.4, 0.7, 0.1, 0.13)
        return self

        """
        输入其他信息
        """
    def broadcast_other_config(self, d):
        print("preview_other_config")
        # select Channel
        d.xpath(start_broadcast_channel_xpath).click()
        d.xpath(start_broadcast_channel_fashion_xpath).wait(timeout=3.0)
        d.xpath(start_broadcast_channel_fashion_xpath).click()
        # select goods
        d.xpath(start_broadcast_toSelect_goods_xpath).click()
        d.xpath(goodsCheckBox_xpath % 1).wait(timeout=3.0)
        for i in range(1,3,1):
            d.xpath(goodsCheckBox_xpath %i).click()
            time.sleep(1)
        d.xpath(confirm_select_goods_xpath).click()
        self.guide_pop_icon_check(d)
        d.xpath(save_select_goods_xpath).click()
        # definition清晰度
        d.xpath(start_broadcast_definition_one_xpath).click()
        return self


        """
        指导弹窗点击
        """
    def guide_pop_icon_check(self, d):
        print("guide_pop_icon_check")
        # select Channel
        if d.xpath(guide_xpath).exists:
            d.xpath(guide_xpath).click()

        """
        添加相册及摄像头权限
        """
    def give_camera_permission(self, d):
        print("give_camera_permission")
        time.sleep(2)
        d.xpath(apply_permission_xpath).wait(timeout=3.0)
        if d.xpath(apply_permission_xpath).exists:
            d.xpath(apply_permission_xpath).click()
            d.xpath(to_give_permission_xpath).click()
            d.xpath(give_permission_using_xpath).click()

        """
        直播间下一步
        """
    def living_next_step(self, d):
        print("living_next_stop")
        if d.xpath(living_next1_xpath).exists:
            d.xpath(living_next1_xpath).click()
        if d.xpath(living_next2_xpath).exists:
            d.xpath(living_next2_xpath).click()
        if d.xpath(living_next3_xpath).exists:
            d.xpath(living_next3_xpath).click()
